const {UserProject} = require('@/mysql/user_project')
const {User} = require('@/mysql/user')
/** 获取当前项目所有用户 */
const getAllTeamer = async ctx => {
    const {project_id} = ctx.request.query
    if(!project_id) return ctx.body = global.toJson(1, '缺少删除项目Id')
    const data = await User.findByProjectId(project_id)
    if(data.length) return ctx.body = data
    return ctx.body = global.toJson(2, '没有找到项目成员信息')

}
const InviteUser = async ctx => {
    const {project_id, user_id} = ctx.request.body
    if(!project_id) return ctx.body=global.toJson(1, '缺少项目Id')
    if(!user_id) return ctx.body=global.toJson(1, '缺少用户Id')
    await UserProject.insertData(user_id, project_id, false)
    return ctx.body = global.toJson(0, '添加成功')
}
const removeUser = async ctx => {
    const id = ctx.params.id
    if(!id) return ctx.body = global.toJson(1, '缺少删除成员Id')
    await UserProject.removeUser(id)
    return ctx.body = global.toJson(0, '删除成员成功')
}

const setAdmin = async ctx => {
    const {auth} = ctx.request.query
    const id =  ctx.params.id
    if(!id) return ctx.body = global.toJson(1, '缺少设置成员Id')
    await UserProject.update({is_admin: auth}, {where: {id}})
    return ctx.body = global.toJson(0, '设置管理员成功')
}
export {getAllTeamer, InviteUser, removeUser, setAdmin}
